/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package administracion.helper;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JTextField;

/**
 *
 * @author Frank
 */
public class Utiles_Validar {

    public static void  isValidoRUC( String ruc ) throws Exception{
        boolean ind = false;
        int[] val_param = {5,4,3,2,7,6,5,4,3,2};

        ruc = ruc.trim();
        if( ruc.length() != 11 ){            
            ind = false;
        }else{
            int suma = 0;
            int resto = -1;
            for( int i = 0; i < 10; i++ ){
                suma += Integer.valueOf("" + ruc.charAt(i)).intValue()*val_param[i];
            }
            resto = suma % 11;
       
            if( 11 - resto == Integer.valueOf("" + ruc.charAt(10)).intValue() ){
                ind = true;
            }else{
                ind = false;
            }
        }
        if(!ind)
            throw new Exception("El RUC ingresado no es válido!");
    }


    public static void isValidoDNI( String dni ) throws Exception {
        boolean ind = false;
        try {
            Integer.parseInt(dni);
            ind = true;
        } catch (NumberFormatException numberFormatException) {
        }
        if(!ind)
            throw new Exception("El DNI ingresado no es válido!");
    }
    public static void isValidoDNINumeros( String dni ) throws Exception {
        boolean ind = false;
        try {
           if(dni.length()!=8){
                throw new Exception("El dni debe tener 8 digitos!");
           }
           else{
               ind=true;
           }
        } catch (Exception numberFormatException) {
        }
        if(!ind)
            throw new Exception("El DNI ingresado no es válido!");
    }


    public static void validaCaracteres(final JTextField jtf,final int num_caracteres){

        jtf.addKeyListener(new KeyAdapter(){
                       public void keyTyped(KeyEvent e){
                               char t=e.getKeyChar();
                       if(!Character.isDigit(t)){
                               e.setKeyChar((char)2);
                        }
                       else if(jtf.getText().trim().length()>(num_caracteres-1))
                                e.setKeyChar((char)2);

                       }
           });
    }
}
